<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class BehaviorLog extends Model
{
    protected $table = 'behavior_log';
    public $timestamps = false;


    const SIGN_YES = 1;
    const SIGN_NO = 2;
    static $SignDisplay = [
        self::SIGN_YES=>'真实用户',
        self::SIGN_NO=>'虚拟用户',
    ];

    /**
     * 添加操作记录
     * @param $user_id
     * @param $field
     * @return array
     */
    static function UserBehaviorLog($user_id, $field, $times = 1)
    {
        $time = time();
        $log_id = BehaviorLog::where('user_id', $user_id)->where('create_time', date('Y-m-d', $time))->value('id');
        //如果有记录
        if ($log_id) {
            if (!BehaviorLog::where('id', $log_id)->increment($field,$times)) {
                return ['status' => 2, 'message' => '添加记录失败'];
            }
        } else {
            if (!BehaviorLog::insert([
                'user_id' => $user_id,
                $field => $times,
                'create_time' => date('Y-m-d', $time),
                'last_update_time' => date('Y-m-d H:i:s', $time),
                'sign' => self::SIGN_NO
            ])) {
                return ['status' => 2, 'message' => '添加记录失败'];
            }
        }
        return ['status' => 1, 'message' => '添加记录成功'];
    }
}
